import axios from 'axios'
import qs from 'qs'

const config = {
    timeout: 50000
}
const service = axios.create(config)

export default class Api {
    static axiosPost(url, data) {
        return new Promise(function (resolve, reject) {
            service({
                method: "post",
                url: url,
                data: data
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    resolve(err);
                });
        })
    }

    static axiosGet(url, data) {
        return new Promise(function (resolve, reject) {
            service({
                method: "get",
                url: url,
                params: data
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    resolve(err);
                });
        })
    }

    static axiosPut(url, data) {
        return new Promise(function (resolve, reject) {
            service({
                method: "put",
                url: url,
                data: data
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    resolve(err);
                });
        })
    }

    static axiosDelete(url, data) {
        return new Promise(function (resolve, reject) {
            service({
                method: "delete",
                url: url,
                params: data
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    resolve(err);
                });
        })
    }

    static axiosPatch(url, data) {
        return new Promise(function (resolve, reject) {
            service({
                method: "patch",
                url: url,
                params: data,
                paramsSerializer: params => {
                    return qs.stringify(params, { indices: false })
                }
            })
                .then(res => {
                    resolve(res.data);
                })
                .catch(err => {
                    resolve(err);
                });
        })
    }
}
// 添加请求拦截器
service.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    config.url = (process.env.VUE_APP_APIURL || '') + config.url
    config.headers = {
        ADMIN_TOKEN: (localStorage.getItem('ADMIN_TOKEN') || '')
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
service.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    response.data = Object.assign({}, {res: response.data}, {status: response.status})
    return response;
}, function (err) {
    // 对响应错误做点什么
    if (err && err.response) {
        switch (err.response.status) {
            case 400:
                err.message = "错误请求";
                err.status = 400
                break;
            case 401:
                err.message = "未授权，请重新登录";
                window.location.href = '/login'
                err.status = 401
                break;
            case 403:
                err.message = "拒绝访问";
                err.status = 403
                break;
            case 404:
                err.message = "请求错误,未找到该资源";
                err.status = 404
                break;
            case 405:
                err.message = "请求方法未允许";
                err.status = 405
                break;
            case 408:
                err.message = "请求超时";
                err.status = 408
                break;
            case 500:
                err.message = "服务器端出错";
                err.status = 500
                break;
            case 501:
                err.message = "网络未实现";
                err.status = 501
                break;
            case 502:
                err.message = "网络错误";
                err.status = 502
                break;
            case 503:
                err.message = "服务不可用";
                err.status = 503
                break;
            case 504:
                err.message = "网络超时";
                err.status = 504
                break;
            case 505:
                err.message = "http版本不支持该请求";
                err.status = 505
                break;
            default:
                err.message = `连接错误${err.response.status}`;
        }
    } else {
        err.message = "连接到服务器失败";
    }
    return Promise.reject(err);
});
